#include"exec.h"
#include"thread.h"
#include"elf32.h"
#include"process.h"
#include"string.h"

extern void intr_exit(void);

int32_t sys_execv(const char* path,char* argv[]){
	uint32_t entry;
	task_tcb* curr;
	intr_stack* intr_s;
	char* filename;
	uint32_t argc=0;
	
	while(argv[argc]){
		argc++;
	}
	entry=load_elf32_exec(path);
	if(!entry){
		return -1;
	}
	filename=strrchr(path,'/')+1;
	curr=current_thread();
	strcpy(curr->name,filename);	
	intr_s=(intr_stack*)((uint32_t)curr+PG_SIZE-sizeof(intr_stack));
	intr_s->eip=(void (*)(void))entry;
	intr_s->esp=PROC_TOP_ADDR;
	intr_s->ecx=argc;
	intr_s->ebx=(uint32_t)argv;
	asm volatile("movl %0,%%esp;jmp intr_exit"::"r"(intr_s));
	return 0;
}
